home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Argus Frameworks 2.1 / Argus Libraries 2.1 / FnAE.cp < prev    next >
Text File  |  1995-12-17  |  13KB  |  402 lines

  1. /**********************************************************************
  2.  
  3.     FnAE.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     You need to make minor modifications to the routines in this
  9.     library to suit your needs.  Areas which you need to look at are
  10.     noted with comments preceded with exclamation marks (!!).  When
  11.     using AppleEvents in application, be sure to set program flag
  12.     HighLevelEvent-Aware to TRUE so system actually sends AppleEvents
  13.     to your application.
  14.     
  15.     Functions Include:
  16.     
  17.       FnAE_InitAE             Installs AE procedures
  18.       FnAE_DoHighLevelEvent   Processes AE
  19.       FnAE_GotRequiredParams  Gets parameters from AE
  20.       FnAE_OpenApp            Open application AE: core suite
  21.       FnAE_OpenDoc            Open document AE: core suite
  22.       FnAE_PrintDoc           Print document AE: core suite
  23.       FnAE_Quit               Quit application AE: core suite
  24.       FnAE_SendOpenAE         Send Open AE to any file or application
  25.       
  26.       // Semi-private
  27.       FnAE_FindProcess
  28. */
  29.  
  30. #include <AppleEvents.h>
  31. #include <Processes.h>
  32. #include <Aliases.h>
  33.  
  34. #define kFinderSig          'FNDR'
  35. #define kAEFinderEvents     'FNDR'
  36. #define kSystemType         'MACS'
  37. #define kAEOpenSelection    'sope'
  38. #define keySelection        'fsel'
  39.  
  40.  
  41. // Prototypes
  42.  
  43. void   FnAE_InitAE( void );
  44. void   FnAE_DoHighLevelEvent(  EventRecord      *theEvent );
  45. OSErr  FnAE_GotRequiredParams( const AppleEvent *theEvent );
  46. pascal OSErr FnAE_OpenApp(     AppleEvent       *theEvent,
  47.                                AppleEvent       *reply,
  48.                                long             refCon );
  49. pascal OSErr FnAE_OpenDoc(     AppleEvent       *theEvent,
  50.                                AppleEvent       *reply,
  51.                                long             refCon );
  52. pascal OSErr FnAE_PrintDoc(    AppleEvent       *theEvent,
  53.                                AppleEvent       *reply,
  54.                                long             refCon );
  55. pascal OSErr FnAE_Quit(        AppleEvent       *theEvent,
  56.                                AppleEvent       *reply,
  57.                                long             refCon );
  58. OSErr  FnAE_SendOpenAE(        FSSpec           *theDoc );
  59.  
  60. OSErr  FnAE_FindProcess( OSType               typeToFind,
  61.                          OSType               creatorToFind,
  62.                          ProcessSerialNumber* processSN );
  63. extern void FnErr_DisplayStr( Str255 s1,
  64.                               Str255 s2,
  65.                               Str255 s3,
  66.                               Str255 s4,
  67.                               int    quitFlag );
  68.  
  69. /********** InitAE */
  70.  
  71. void FnAE_InitAE( void )
  72. {
  73.     OSErr    errCode;
  74.  
  75.     errCode = AEInstallEventHandler( kCoreEventClass, 
  76.                                      kAEOpenApplication, 
  77.                                      (AEEventHandlerProcPtr)FnAE_OpenApp, 
  78.                                      0, 
  79.                                      false);
  80.                                                                             
  81.     errCode = AEInstallEventHandler( kCoreEventClass, 
  82.                                      kAEOpenDocuments, 
  83.                                      (AEEventHandlerProcPtr)FnAE_OpenDoc, 
  84.                                      0, 
  85.                                      false);        
  86.                                                                     
  87.     errCode = AEInstallEventHandler( kCoreEventClass, 
  88.                                      kAEPrintDocuments, 
  89.                                      (AEEventHandlerProcPtr)FnAE_PrintDoc, 
  90.                                      0, 
  91.                                      false);        
  92.                                                 
  93.     errCode = AEInstallEventHandler( kCoreEventClass, 
  94.                                      kAEQuitApplication, 
  95.                                      (AEEventHandlerProcPtr)FnAE_Quit, 
  96.                                      0, 
  97.                                      false);
  98. }
  99.  
  100.  
  101. /********** DoHighLevelEvent */
  102.  
  103. void FnAE_DoHighLevelEvent( EventRecord *theEvent )
  104. {
  105.     OSErr    errCode;
  106.     /*
  107.         Check to see if the Apple event is of a user-defined class;
  108.         if so, handle it.  Otherwise, call AEProcessAppleEvent.
  109.     */
  110.     errCode = AEProcessAppleEvent( theEvent );
  111.     if( errCode == errAEEventNotHandled )
  112.     {
  113.         // !!??  Acknowledge( CantHandleAEVTID );
  114.         // FnErr_DisplayStrID( 903, FALSE );
  115.     }
  116. }
  117.  
  118.  
  119. /********** GotRequiredParams */
  120.  
  121. OSErr FnAE_GotRequiredParams( const AppleEvent *theEvent )
  122. {
  123.     OSErr    errCode;
  124.     Size     actualSize;
  125.     DescType returnedType;
  126.  
  127.     errCode = AEGetAttributePtr( theEvent, 
  128.                                  keyMissedKeywordAttr, 
  129.                                  typeWildCard,
  130.                                  &returnedType, 
  131.                                  NULL, 
  132.                                  0, 
  133.                                  &actualSize);
  134.     if (errCode == errAEDescNotFound)
  135.     {        
  136.         // no parameters => no error
  137.         errCode = noErr;
  138.     }
  139.     else if (errCode == noErr)
  140.     {            
  141.         // got a parameter => it wasn't handled
  142.         errCode = errAEEventNotHandled;
  143.     }
  144.     return (errCode);
  145. }
  146.  
  147.  
  148. /********** OpenApp */
  149.  
  150. pascal OSErr FnAE_OpenApp( AppleEvent *theEvent,
  151.                            AppleEvent *reply,
  152.                            long       refCon )
  153. {
  154.     OSErr    errCode;
  155.  
  156.     errCode = FnAE_GotRequiredParams( theEvent );
  157.     if (errCode == noErr)
  158.     {
  159.         // !! call open application procedure
  160.         
  161.         // !! define prototype
  162.         extern void NewWindow_( void );
  163.  
  164.         // !! now call procedure
  165.         NewWindow_();
  166.     }
  167.     return (errCode);
  168. }
  169.  
  170.  
  171. /********** OpenDoc */
  172.  
  173. pascal OSErr FnAE_OpenDoc( AppleEvent *theEvent,
  174.                            AppleEvent *reply,
  175.                            long       refCon )
  176. {
  177.     OSErr        errCode;
  178.     OSErr        ignoreErr;
  179.     AEDescList   docList;
  180.     long         itemsInList;
  181.     long         index;
  182.     AEKeyword    keyword;
  183.     DescType     returnedType;
  184.     Size         actualSize;
  185.     FSSpec       myFSS;
  186.     short        wdRefNum;
  187.  
  188.     errCode = AEGetParamDesc( theEvent,
  189.                               keyDirectObject,
  190.                               typeAEList,
  191.                               &docList );
  192.     if( errCode == noErr )
  193.     {
  194.         errCode = FnAE_GotRequiredParams( theEvent );
  195.         if( errCode == noErr )
  196.         {
  197.             errCode = AECountItems( &docList, &itemsInList );
  198.             if( errCode == noErr )
  199.             {
  200.                 for( index = 1; index <= itemsInList; index++ )
  201.                 {
  202.                     errCode = AEGetNthPtr( &docList,
  203.                                            index,
  204.                                            typeFSS,
  205.                                            &keyword,
  206.                                            &returnedType,
  207.                                            (Ptr)&myFSS,
  208.                                            sizeof(myFSS),
  209.                                            &actualSize );
  210.                     if( errCode == noErr )
  211.                     {
  212.                         // !! open window and file
  213.                         /***
  214.                         errCode = OpenWD( myFSS.vRefNum,
  215.                                           myFSS.parID,
  216.                                           0,
  217.                                           &wdRefNum );
  218.                         if( errCode == noErr )
  219.                         {
  220.                             OpenDoc( myFSS.name, wdRefNum );
  221.                         }
  222.                         ***/
  223.                     }
  224.                 }
  225.             }
  226.         }
  227.         ignoreErr = AEDisposeDesc( &docList );
  228.     }
  229.     return (errCode);
  230. }
  231.  
  232.  
  233. /********** PrintDoc */
  234.  
  235. pascal OSErr FnAE_PrintDoc( AppleEvent *theEvent,
  236.                             AppleEvent *reply,
  237.                             long       refCon )
  238. {
  239.     OSErr    errCode;
  240.  
  241.     errCode = FnAE_GotRequiredParams( theEvent );
  242.     if( errCode == noErr )
  243.     {
  244.         // !!??  Acknowledge( CantHandleAEVTID );
  245.     }
  246.     return( errCode );
  247. }
  248.  
  249.  
  250. /********** Quit */
  251.  
  252. pascal OSErr FnAE_Quit( AppleEvent *theEvent,
  253.                         AppleEvent *reply,
  254.                         long       refCon )
  255. {
  256.     OSErr    errCode;
  257.  
  258.     errCode = FnAE_GotRequiredParams( theEvent );
  259.     if( errCode == noErr )
  260.     {
  261.         // !! call application quit procedure
  262.         
  263.         // !! define prototype
  264.         extern void Quit_( void );
  265.  
  266.         // !! now call procedure
  267.         Quit_();
  268.     }
  269.     return( errCode );
  270. }
  271.  
  272.  
  273. /********** SendOpenAE */
  274.  
  275. OSErr FnAE_SendOpenAE( FSSpec *theDoc )
  276. {
  277.     AppleEvent          aeEvent;        // the event to create
  278.     AEDesc              myAddressDesc;  // descriptors for the AE
  279.     AEDesc              aeDirDesc;
  280.     AEDesc              listElem;
  281.     AEDesc              fileList;       // our list
  282.     FSSpec              dirSpec;
  283.     AliasHandle         dirAlias;       // alias to directory
  284.     AliasHandle         fileAlias;      // alias of the file itself
  285.     ProcessSerialNumber process;        // the finder's psn
  286.     OSErr               myErr;          // duh
  287.  
  288.     // Get the psn of the Finder and create the target address for AE
  289.     if(FnAE_FindProcess(kFinderSig,kSystemType,&process))
  290.     {
  291.         FnErr_DisplayStr(
  292.             "\pThe Finder must be running in order ",
  293.             "\pto be able to send Apple Event.",
  294.             "\p",
  295.             "\p",
  296.             FALSE );
  297.         return procNotFound;
  298.     }
  299.     myErr = AECreateDesc( typeProcessSerialNumber,
  300.                           (Ptr)&process,
  301.                           sizeof(process),
  302.                           &myAddressDesc );
  303.     if(myErr)
  304.         return myErr;
  305.  
  306.     // Create an empty AppleEvent
  307.     myErr = AECreateAppleEvent( kAEFinderEvents, 
  308.                                 kAEOpenSelection,
  309.                                 &myAddressDesc, 
  310.                                 kAutoGenerateReturnID,
  311.                                 kAnyTransactionID,
  312.                                 &aeEvent );
  313.     if(myErr)
  314.         return myErr;
  315.  
  316.     // Make an FSSpec and alias for the parent folder, and for the file
  317.     FSMakeFSSpec(theDoc->vRefNum,theDoc->parID,nil,&dirSpec);
  318.     NewAlias(nil,&dirSpec,&dirAlias);
  319.     NewAlias(nil,theDoc,&fileAlias);
  320.  
  321.     // Create the file list.
  322.     myErr=AECreateList(nil,0,false,&fileList);
  323.     if(myErr)
  324.         return myErr;
  325.  
  326.     // Create the folder descriptor
  327.     HLock((Handle)dirAlias);
  328.     AECreateDesc(typeAlias, (Ptr) *dirAlias, GetHandleSize
  329.                     ((Handle) dirAlias), &aeDirDesc);
  330.     HUnlock((Handle)dirAlias);
  331.     DisposHandle((Handle)dirAlias);
  332.  
  333.     if((myErr = AEPutParamDesc(&aeEvent,keyDirectObject,&aeDirDesc)) ==
  334.         noErr)
  335.     {
  336.         AEDisposeDesc(&aeDirDesc);
  337.         HLock((Handle)fileAlias);
  338.  
  339.         AECreateDesc(typeAlias, (Ptr)*fileAlias,
  340.             GetHandleSize((Handle)fileAlias), &listElem);
  341.         HUnlock((Handle)fileAlias);
  342.         DisposHandle((Handle)fileAlias);
  343.         myErr = AEPutDesc(&fileList,0,&listElem);
  344.     }
  345.     if(myErr)
  346.         return myErr;
  347.     AEDisposeDesc(&listElem);
  348.  
  349.     myErr = AEPutParamDesc(&aeEvent,keySelection,&fileList);
  350.     if(myErr)
  351.         return myErr;
  352.  
  353.     myErr = AEDisposeDesc(&fileList);
  354.  
  355.     myErr = AESend(&aeEvent, nil,
  356.             kAENoReply+kAEAlwaysInteract+kAECanSwitchLayer,
  357.             kAENormalPriority, kAEDefaultTimeout, nil, nil);
  358.     AEDisposeDesc(&aeEvent);
  359.     
  360.     return noErr;
  361. }
  362.  
  363.  
  364. /********** FindProcess */
  365.  
  366. OSErr FnAE_FindProcess( OSType                  typeToFind, 
  367.                         OSType                  creatorToFind,
  368.                         ProcessSerialNumberPtr  processSN )
  369. {
  370.     ProcessInfoRec  tempInfo;
  371.     FSSpec          procSpec;
  372.     Str31           processName;
  373.     OSErr           myErr = noErr;
  374.     Boolean         done;
  375.  
  376.     // start at the beginning of the process list
  377.     processSN->lowLongOfPSN = kNoProcess;
  378.     processSN->highLongOfPSN = kNoProcess;
  379.  
  380.     // initialize the process information record
  381.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  382.     tempInfo.processName = (StringPtr)&processName;
  383.     tempInfo.processAppSpec = &procSpec;
  384.  
  385.     done = FALSE;
  386.     while( done == FALSE )
  387.     {
  388.         myErr = GetNextProcess( processSN );
  389.         if( myErr == noErr )
  390.             GetProcessInformation( processSN, &tempInfo );
  391.         else
  392.             done = TRUE;
  393.             
  394.         if( tempInfo.processSignature == creatorToFind ||
  395.             tempInfo.processType == typeToFind )
  396.             done = TRUE;
  397.     }
  398.     
  399.     return(myErr);
  400. }
  401.  
  402. // End of File